﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class TariffBLL : IBaseInfo
    {
        private static readonly ICalendarGroupDA calendarGroupDA = DataAccess.CreateCalendarGroupDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IRateDA rateDA = DataAccess.CreateRateDA();
        private static readonly ITariffDA tariffDA = DataAccess.CreateTariffDA();
        private static readonly ITariffDetailDA tariffDetailDA = DataAccess.CreateTariffDetailDA();

        public TariffBLL()
        {
            this.pubParasInfo = null;
        }

        public TariffBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TariffBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<TariffInfo> TariffInfos = new List<TariffInfo>();
                TariffInfo tariffInfo = null;
                foreach (string id in IDS)
                {
                    tariffInfo = tariffDA.GetTariffByID(dataBase, tran, id, this.dataBaseID[0]);
                    TariffInfos.Add(tariffInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (tariffDA.DeleteTariff(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<TariffInfo>(dataBase, tran, TariffInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "TARIFF", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return tariffDA.GetAllTariff(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return tariffDA.GetTariffByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            TariffQueryEntity tariffQueryEntity = obj as TariffQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(tariffQueryEntity.TariffID))
            {
                sqlWhere.Add(" TARIFF_ID " + tariffQueryEntity.TariffID);
            }
            if (!string.IsNullOrEmpty(tariffQueryEntity.Descr))
            {
                sqlWhere.Add(" DESCR " + tariffQueryEntity.Descr);
            }
            if (!string.IsNullOrEmpty(tariffQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + tariffQueryEntity.IsActive);
            }
            return tariffDA.GetTariffByQueryList(sqlWhere, tariffQueryEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            TariffInfo tariffInfo = obj as TariffInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                if ((tariffInfo.TariffType == TariffType.CalendarGroup) && !calendarGroupDA.CheckCalendarGroupIDUnique(dataBase, tran, tariffInfo.TypeCode, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9140_001", this.pubParasInfo.UserLanguage, new List<string> { tariffInfo.TypeCode });
                }
                if ((tariffInfo.TariffType == TariffType.Fixed) && !rateDA.CheckRateIDUnique(dataBase, tran, tariffInfo.TypeCode, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9140_004", this.pubParasInfo.UserLanguage, new List<string> { tariffInfo.TypeCode });
                }
                if (string.IsNullOrEmpty(tariffInfo.TariffID))
                {
                    tariffInfo.TariffID = commonMethod.GenNextNumStr(dataBase, tran, "TARIFF", whLoginID);
                }
                tariffInfo.UpdatedBy = tariffInfo.CreatedBy;
                tariffInfo.CreatedDate = DateTime.Now;
                tariffInfo.UpdatedDate = DateTime.Now;
                if (tariffDA.InsertTariff(dataBase, tran, tariffInfo, whLoginID) > 0)
                {
                    strContractID = tariffInfo.TariffID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "TARIFF", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }

        public string Update(object obj)
        {
            TariffInfo tariffInfo = obj as TariffInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                TariffInfo oldTariffInfo = tariffDA.GetTariffByID(dataBase, tran, tariffInfo.TariffID, this.dataBaseID[0]);
                if (tariffDA.UpdateTariff(dataBase, tran, tariffInfo, whLoginID) > 0)
                {
                    strContractID = tariffInfo.TariffID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, oldTariffInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "TARIFF", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }
    }
}

